//
//  EMLoadTableHeaderView.m
//  EasyMoney
//
//  Created by zh on 2020/1/11.
//  Copyright © 2020 zh. All rights reserved.
//

#import "EMLoadTableHeaderView.h"

@interface EMLoanHeaderItemView : UIView

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *iconImageView;

- (void)initWithItemView:(NSString *)titleString andIconImage:(NSString *)iconName;

@end

@implementation EMLoanHeaderItemView

- (void)initWithItemView:(NSString *)titleString{

    self.iconImageView = [[UIImageView alloc]init];
    [self addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.bottom.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];

    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.text = titleString;
    self.titleLabel.textColor = ColorFromRGB(0x2F5AFF);
    self.titleLabel.font = FONT_PINGFANG_REGULAR_(13);
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(2);
        make.right.equalTo(self);
        make.centerY.equalTo(self);
    }];
}



@end

@interface EMLoadTableHeaderView ()

@property (nonatomic, strong) UIView *whiteBgView; //白色背景
@property (nonatomic, strong) UILabel *topLabel; //
@property (nonatomic, strong) UILabel *countLabel; //金额
@property (nonatomic, strong) EMLoanHeaderItemView *dateItemView; //时间
@property (nonatomic, strong) UILabel *joinLabel; //参与
@property (nonatomic, strong) EMLoanHeaderItemView *leftIconView; //左边iconView;
@property (nonatomic, strong) EMLoanHeaderItemView *rightIconView; //右边iconView;


@end

@implementation EMLoadTableHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.backgroundColor = [UIColor clearColor];
    self.whiteBgView = [[UIView alloc]initWithFrame:CGRectMake(16, 0, SCREEN_WIDTH-32, 306)];
    self.whiteBgView.layer.cornerRadius = 8;
    self.whiteBgView.backgroundColor = ColorFromRGB(0xffffff);
    self.whiteBgView.layer.shadowColor = ColorFromRGBWithAlpha(0x2F5AFF, 0.1).CGColor;
    self.whiteBgView.layer.shadowOffset = CGSizeMake(0,2);
    self.whiteBgView.layer.shadowOpacity = 1;
    self.whiteBgView.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(16, 0, SCREEN_WIDTH-32, 306)].CGPath;
    self.whiteBgView.layer.shadowRadius = 10;
    [self addSubview:self.whiteBgView];

    self.contentBtn = [[UIButton alloc]init];
    [self addSubview:self.contentBtn];
    [self.contentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    self.topLabel = [[UILabel alloc]init];
    self.topLabel.numberOfLines = 0;
    self.topLabel.font = FONT_PINGFANG_REGULAR_(14);
    self.topLabel.textColor = ColorFromRGB(0x333333);
    [self.contentBtn addSubview:self.topLabel];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(42);
        make.centerX.equalTo(self.whiteBgView);
    }];

    //上线金额view
    UIView *limitView = [[UIView alloc]init];
    [self.whiteBgView addSubview:limitView];
    [limitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.whiteBgView);
        make.top.equalTo(self.topLabel.mas_bottom).offset(5);
    }];
    UILabel *unitLabel = [[UILabel alloc]init];
    unitLabel.text = NSLocalizedString(@"¥", nil);
    unitLabel.textColor = ColorFromRGB(0x333333);
    unitLabel.font = FONT_PINGFANG_MEDIUM_(24);
    [limitView addSubview:unitLabel];
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(limitView);
        make.centerY.equalTo(limitView);
    }];

    self.countLabel = [[UILabel alloc]init];
    self.countLabel.textColor = ColorFromRGB(0x333333);
    self.countLabel.font = FONT_PINGFANG_MEDIUM_(36);
    [limitView addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(unitLabel.mas_right).offset(5);
        make.centerY.equalTo(limitView);
        make.height.equalTo(limitView);
        make.right.equalTo(limitView.mas_right);
    }];

    self.dateItemView = [[EMLoanHeaderItemView alloc]init];
    [self.dateItemView initWithItemView:NSLocalizedString(@"", nil)];
    self.dateItemView.iconImageView.image = [UIImage imageNamed:@"homeicon_3(1)"];
    self.dateItemView.titleLabel.textColor = ColorFromRGB(0xA8A8A8);
    [self.whiteBgView addSubview:self.dateItemView];
    [self.dateItemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.countLabel.mas_bottom).offset(7);
        make.centerX.equalTo(self.whiteBgView);
    }];

    UIImageView *joinImageView = [[UIImageView alloc]init];
    UIImage *oriImage = [UIImage gradientImageSize:CGSizeMake(SCREEN_WIDTH-32-90, 44) colors:@[[UIColor colorwithHexString:@"#36B2FF"],[UIColor colorwithHexString:@"#2F5AFF"]] startPoint:CGPointMake(0, 22) endPoint:CGPointMake(SCREEN_WIDTH-16-45, 22)];
    UIImage *resultImage = [oriImage roundImageWithCornerRadius:22];
    joinImageView.image = resultImage;
    [self.whiteBgView addSubview:joinImageView];
    [joinImageView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(45);
         make.right.mas_equalTo(-45);
         make.height.mas_equalTo(44);
         make.top.equalTo(self.dateItemView.mas_bottom).mas_offset(34);
     }];

    self.joinLabel = [[UILabel alloc]init];
    self.joinLabel.textColor = ColorFromRGB(0xffffff);
    self.joinLabel.font = FONT_PINGFANG_MEDIUM_(18);
    [joinImageView addSubview:self.joinLabel];
    [self.joinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(joinImageView);
    }];

    UIView *bgView = [[UIView alloc] init];
    bgView.layer.backgroundColor = [UIColor colorWithRed:47/255.0 green:90/255.0 blue:255/255.0 alpha:0.06].CGColor;
    bgView.layer.cornerRadius = 6;
    bgView.layer.masksToBounds = YES;
    [self.whiteBgView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(19);
        make.right.mas_equalTo(-19);
        make.height.mas_equalTo(36);
        make.top.equalTo(joinImageView.mas_bottom).offset(32);
    }];

    EMLoanHeaderItemView * leftIconView = [[EMLoanHeaderItemView alloc]init];
    self.leftIconView = leftIconView;
    [leftIconView initWithItemView:NSLocalizedString(@"", nil)];
    [bgView addSubview:leftIconView];
    [leftIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgView);
        make.centerX.equalTo(bgView).multipliedBy(0.5);
    }];

    EMLoanHeaderItemView * rightIconView = [[EMLoanHeaderItemView alloc]init];
    self.rightIconView = rightIconView;
    [rightIconView initWithItemView:NSLocalizedString(@"", nil)];
    [bgView addSubview:rightIconView];
    [rightIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgView);
        make.centerX.equalTo(bgView).multipliedBy(1.5);
    }];

}



- (void)setModel:(EMLoanResponseModel *)model{
    _model = model;
    self.topLabel.text = model.productInfo.productName;
    self.countLabel.text = model.productInfo.upperLimit;
    self.dateItemView.titleLabel.text = model.productInfo.termRange;
    self.joinLabel.text = model.productInfo.buttonText;
    self.leftIconView.titleLabel.text = model.leftCenterText;
    self.rightIconView.titleLabel.text = model.rightCenterText;
    [self.leftIconView.iconImageView yy_setImageWithURL:[NSURL URLWithString:model.leftCenterIcon] options:YYWebImageOptionAllowInvalidSSLCertificates];
     [self.rightIconView.iconImageView yy_setImageWithURL:[NSURL URLWithString:model.rightCenterIcon] options:YYWebImageOptionAllowInvalidSSLCertificates];
}

@end
